package br.com.simulador.session;

import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

import br.com.simulador.model.ItemQuestao;
import br.com.simulador.model.Questao;

@Name("questaoHome")
public class QuestaoHome extends EntityHome<Questao> {

	@In(create=true)
	private SimuladoHome simuladoHome;
	private Long simuladoId;
	
	public void setSimuladoId(Long id){
		simuladoId = id;
		simuladoHome.setId(id);
		simuladoHome.wire();
	}
	
	public void setQuestaoId(Long id) {
		setId(id);
	}

	public Long getQuestaoId() {
		return (Long) getId();
	}

	@Override
	protected Questao createInstance() {
		Questao questao = new Questao();
		return questao;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
	}

	public boolean isWired() {
		return true;
	}

	public Questao getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public List<ItemQuestao> getListQuestoes() {
		return getInstance() == null ? null : new ArrayList<ItemQuestao>(
				getInstance().getListItemQuestoes());
	}

	@Override
	public String persist() {
		String retorno = super.persist();
		if(simuladoHome.isWired()){
			simuladoHome.getInstance().getQuestoes().add(getInstance());
			
			simuladoHome.persist();
		}
		return retorno;
	}

	@Override
	public String update() {
		String retorno = super.update();
		if(simuladoHome.isWired()){
			simuladoHome.getInstance().getQuestoes().add(getInstance());
			simuladoHome.persist();
		}
		return retorno;
	}

	public Long getSimuladoId() {
		return simuladoId;
	}
	
	
	public String newItemQuestao(){
		ItemQuestao result = new ItemQuestao();
		result.setQuestao(getInstance());
		getInstance().getListItemQuestoes().add(result);
		return null;
	}
	

}
